package 环形链表;

public class Node1 {
    static class Node {
        Node next;
        Node pre;
        int value;

        public Node(Node pre, int value, Node next) {//传参
            this.pre = pre;
            this.value = value;
            this.next = next;
        }
    }
    public Node sentinel=new Node(null,-1,null);
    public Node1(){
        sentinel.pre=sentinel;
        sentinel.next=sentinel;//头与尾分别指向自己
    }
    Node head=sentinel.pre;
    public void addFirst(int value){
        Node a=sentinel;
        Node b=sentinel.next;
        Node add=new Node(a,value,b);
        a.next=add;
        b.pre=add;
    }
    public void addLast(int value){
        Node a=sentinel.pre;
        Node b=sentinel;
        Node added=new Node(a,value,b);
        a.next=added;
        b.pre=added;
    }
    public void removeFirst(){
        Node removed=sentinel.next;
        if(removed==sentinel){
            throw new IllegalArgumentException(
                    String.format("不合法")
            );
        }
        Node a=sentinel;
        Node b=removed.next;
        a.next=b;
        b.pre=a;
    }
    private Exception illegalIndex(int index) {
        throw new IllegalArgumentException(
                String.format(" 不合法")
        );
    }
    public void removeLast(){//要删除最后一个就把哨兵当成尾
        Node removed=sentinel.pre;
        if (removed==sentinel){//说明链表内部存在内容
            throw new IllegalArgumentException(
                    String.format("不合法")
            );
        }
        Node a=removed.pre;
        Node b=sentinel;
        a.next=b;
        b.pre=a;
    }
    public Node findByValue(int value){
        Node p=sentinel.next;
        while(p!=sentinel){
            if(p.value==value){
                return p;
            }
        }
        return null;
    }
    public  void removeByValue(int value){
        Node removed=findByValue(value);
        if(removed==null){
            return;
        }
        Node a=removed.pre;
        Node b=removed.next;
        a.next=b;
        b.pre=a;
    }

    public void loop1(){
        recsion(head);
    }
    public void  recsion(Node curr){
        if(curr==null){
            return;
        }
        System.out.println(curr.value);
        recsion(curr.next);
    }


}
